<?php
/**
 * file manualcontroller.php
 * Created on 2015-4-13 下午8:43:11 
 * Create by xiweijie
 * @msg : 新版频道页精品栏目获取规则
 * @control : <controller data='{"id":56,"limit":"6","tagId":"310","proLevel":"4,6"}'>
 */
namespace haibao\cms\business\control\pc\channel;
use \haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;

class ExclusiveController extends ChannelController{
    
    public function __construct($updateTime = null,$controlId,$isPublish){
        $this->savaLog();
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        
        $articleIds = $articleList = $autoArticleList = array();
        $tagId = $controlData['manual']['tagId'];
        if($controlData['auto']){
            // DO : 公共方法解析综合数据(考虑存在复合数据需要剔除)
            if(isset($controlData['auto'])) {
                foreach($controlData['auto'] as $auto){
                    $paramVal = json_decode($auto['ParameterValue'],true);
                    $infoId = explode(',',$paramVal['multiData']);
                    $temp['Id'] = $infoId[0];
                    $temp['IdType'] = $infoId[1];
                    $articleIds[] = $temp;
                }
                $controlData['manual']['proLevel'] = \haibao\cms\model\data\ArticleProImage::Level_SIX.','.\haibao\cms\model\data\ArticleProImage::Level_FOUR;
                $this->parseMultiData($controlData['auto'],$controlData['manual'],true);
            }
            $tagIdArr = array();
            foreach($this->packageArr as $article){
                if(isset($article['Entity'])){
                    $entity = $article['Entity'];
                    if($article['Type'] == self::DATA_TYPE_ARTICLE){
                        array_push($tagIdArr,$entity->ArticleTag->TagId);
                    }elseif($article['Type'] == self::DATA_TYPE_IMAGE_SET){
                        array_push($tagIdArr,$entity->ImageCollectiveTag->TagId);
                    }
                }
            }
            $tagInfo = PublicControllerBusiness::getTagInfoById($tagIdArr,'');//手动的标签信息
            $topTag = self::getTopTags();
            $i=1;
            foreach($this->packageArr as $key=>$article){
                if($tagId == 24){
                    $proLevel = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
                } else {
                    if($i<=3){
                        $proLevel = \haibao\cms\model\data\ArticleProImage::Level_SIX;
                    }else{
                        $proLevel = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
                    }
                }

            	$i++;
                if($article['Type'] == self::DATA_TYPE_ARTICLE || $article['Type'] == self::DATA_TYPE_IMAGE_SET){
                    $entity = $article['Entity'];
                    $temp = array();
                    
                    if($entity->SiteType == self::DATA_TYPE_ARTICLE){
                        $temp['TagUrl'] = $tagInfo[$entity->ArticleTag->TagId]['Url'];
                        $temp['TagName'] = $tagInfo[$entity->ArticleTag->TagId]['TagName'];
                        $temp['ChannelUrl'] = self::getArticleUrl($entity->PublishId,$entity->ArticleTag->ParentId);
                        if(is_array($entity->ArticleProImage)){
                        	$temp['ProImage'] = isset($entity->ArticleProImage[$proLevel])?$entity->ArticleProImage[$proLevel]['PicUrl']:(isset($entity->ArticleProImage[\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$entity->ArticleProImage[\haibao\cms\model\data\ArticleProImage::Level_ZERO]['PicUrl']:'');
                        }else{
                        	$temp['ProImage'] = $entity->ArticleProImage;
                        }
                        
                        $temp['SiteType'] = $entity->SiteType;
                    }else{
                        $temp['TagUrl'] = $tagInfo[$entity->ImageCollectiveTag->TagId]['Url'];
                        $temp['TagName'] = $tagInfo[$entity->ImageCollectiveTag->TagId]['TagName'];
                        $temp['ChannelUrl'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/'.$topTag[$entity->ImageCollectiveTag->ParentId]['channel'].'/pics/'.$entity->Id.'.htm';
                        if(is_array($entity->ImageCollectivePro)){
                            $temp['ProImage'] = isset($entity->ImageCollectivePro[$proLevel])?$entity->ImageCollectivePro[$proLevel]['Filename']:(isset($entity->ImageCollectivePro[\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO])?$entity->ImageCollectivePro[\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO]['Filename']:'');
                        }else{
                            $temp['ProImage'] = $entity->ImageCollectivePro;
                        }
                        $temp['SiteType'] = self::DATA_TYPE_IMAGE_SET;
                    }
                    $temp['Title'] = $entity->Title;
                    $temp['ShortTitle'] = $entity->ShortTitle;
                    $temp['Id'] = $entity->Id;
                    array_push($autoArticleList,$temp);
                }
            }
        }
        $articleList = $bottomList = array();
        $this->parseTagId($controlData['manual']['tagId']);
        $this->packageArrCount = count($autoArticleList);
        if($this->packageArrCount < 6){
            $articleList = $this->getExclusiveList($controlData['manual'],$articleIds,$tagId);
        }
        $articleLists = array_merge($autoArticleList,$articleList);
        $this->assign('topList',array_slice($articleLists, 0,3));
        if($tagId == 24){
           $bottomList = array_slice($articleLists, 0,3);
        } else {
            $bottomList = array_slice($articleLists, 3,3);
        }
        $this->assign('bottomList',$bottomList);

        $this->assign('tagId', $tagId);
        
        $path = $this->generateHtmlPath(__CLASS__);
        $isNewHtml = $this->generateHtml($path, $content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    public function getExclusiveList($manualData,$articleIds,$tagId = 0){
    	$tagIdArr = $this->getExclusiveTagList($manualData['tagId']);
    	$articleEntity = new ArticleEntity();
    	$articleEntity->setLevel(3);
    	$articleEntity->setBigLevel(7);
    	$articleEntity->setLimit($manualData['limit']-$this->packageArrCount);
    	$articleEntity->setNotInId($articleIds);
    	$articleEntity->setIsProImage(true);
    	$articleEntity->setIsLeftJoinTag(true);
    	$articleEntity->setIsConditionTag(true);
    	$articleEntity->setIsReturnTag(true);
    	$articleEntity->setIsImageColl(false);
    	$articleEntity->setSource(__CLASS__);
    	$articleEntity->setIsDebug(false);
    	$articleEntity->setIsOnlyReturnPrimaryTag(true);
    	$articleEntity->setIsDate(true, \haibao\cms\business\control\pc\action\ArticleAction::DATE_TYPE_TWO_DAYS_AGO);
    	$articleEntity->setOrderByNew(true, \haibao\cms\business\control\pc\action\ArticleAction::SORT_DESC);

    	$articleEntity->setIsPrimaryTag(true);
    	if($tagIdArr){
    		$articleEntity->setIsParentTag(false);
    		$articleEntity->setTagIds($tagIdArr);
    	}else{
    		$articleEntity->setIsParentTag(true);
    		$articleEntity->setTagIds((int)$manualData['tagId']);
    	}
    	
    	if(!isset($manualData['proLevel'])) $manualData['proLevel'] = 6;
    	$articleEntity->setProImageLevel(explode(',', $manualData['proLevel']));
    	$this->articleAction->setArticleEnity($articleEntity);
    	list($articles,$articleProImages,$tagInfo) = $this->articleAction->getListForNew();
    	$articleList = array();
    	$i=1;
        foreach($articles as $key=>$article){
        	$temp = array();
            $temp['TagUrl'] = $tagInfo[$article->ArticleTag->TagId]['Url'];
            $temp['TagName'] = $tagInfo[$article->ArticleTag->TagId]['TagName'];
            $temp['ChannelUrl'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
        	$temp['SiteType'] = $article->SiteType;
        	$temp['Title'] = $article->Title;
        	$temp['ShortTitle'] = $article->ShortTitle;
        	$temp['Id'] = $article->Id;
        	if($tagId == 24){
                $proLevel = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
            } else {
                if($i<=3-$this->packageArrCount){
                    $proLevel = \haibao\cms\model\data\ArticleProImage::Level_SIX;
                }else{
                    $proLevel = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
                }
            }

        	
        	$temp['ProImage'] = isset($articleProImages[$article->Id][$proLevel])?$articleProImages[$article->Id][$proLevel]:
            	(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
        	
            array_push($articleList,$temp);
            $i++;
        }
        return $articleList;
    }
    

    private function getExclusiveTagList($topTagId){
    	$arr = array(
    			310=>array(8411,9359,7183,8433,7286,11368),
    			1383=>array(),
    			24=>array(11044,8065),
    			955=>array(11195,9618),
    			6863=>array(10057,8992,11001,6875),
    			7455=>array(7959,8191),
    			520=>array(),
    			211=>array()
    	);
    	return $arr[$topTagId];
    }
}